package com.yuke.cloud.service.omc.service.impl;

import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.omc.dto.OdsTeamSpecInfoDto;
import com.yuke.cloud.service.api.uac.dto.UserInfoDto;
import com.yuke.cloud.service.api.uac.service.UacUserFeignApi;
import com.yuke.cloud.service.api.wmc.dto.StorageDto;
import com.yuke.cloud.service.api.wmc.dto.WarehouseInfoDto;
import com.yuke.cloud.service.api.wmc.service.WmcStorageFeignApi;
import com.yuke.cloud.service.omc.dao.OdsTeamMapper;
import com.yuke.cloud.service.omc.dto.OdsTeamDto;
import com.yuke.cloud.service.omc.dto.OdsTeamGoodsDto;
import com.yuke.cloud.service.omc.dto.OdsTeamInfoDto;
import com.yuke.cloud.service.omc.dto.OdsTeamRecordDto;
import com.yuke.cloud.service.omc.entity.OdsTeam;
import com.yuke.cloud.service.omc.service.OdsTeamGoodsService;
import com.yuke.cloud.service.omc.service.OdsTeamService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;


/**
 * Created by zn on 2020/09/26.
 */
@Service
@Transactional
public class OdsTeamServiceImpl extends BaseService<OdsTeam> implements OdsTeamService {
    @Resource
    private OdsTeamMapper odsTeamMapper;
    @Resource
    private OdsTeamGoodsService odsTeamGoodsService;
    @Resource
    private UacUserFeignApi uacUserFeignApi;
    @Resource
    private WmcStorageFeignApi wmcStorageFeignApi;
    @Resource
    private RedisTemplate redisTemplate;

    //设定几点截团
    private final static Integer nextHouse = 9;
    //设定成团件数要求，0不做要求；
    private final static Integer teamNum = 0;
    //设定成团总金额要求
    private final static Integer teamMoney = 0;
    //设定个人最低件数
    private final static Integer minNum = 0;
    //设定个人最低金额
    private final static Integer minMoney = 20;


    @Override
    public int saveTeamInfo(OdsTeam odsTeam){

        if (odsTeam.getTeamType().equals((byte)1)){
            //拼单购
            odsTeam.setStartTime(new Date());
            String id = "TeamShareInfo:" + odsTeam.getTeamId();
            redisTemplate.opsForValue().set(id, odsTeam, 2*24*60*60, TimeUnit.SECONDS);
        }else{
            //团购
            Date date = new Date();
            odsTeam.setStartTime(date);

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE,+1);
            calendar.set(Calendar.HOUR_OF_DAY, nextHouse); //将小时至0
            calendar.set(Calendar.MINUTE, 0); //将分钟至0
            calendar.set(Calendar.SECOND,0); //将秒至0
            calendar.set(Calendar.MILLISECOND, 0); //将毫秒至0

            odsTeam.setEndTime(calendar.getTime());
            odsTeam.setTeamNum(teamNum);
            odsTeam.setTeamMoney(teamMoney);
            odsTeam.setMinNum(minNum);
            odsTeam.setMinMoney(minMoney);
            Long expireTime = (calendar.getTime().getTime() - date.getTime()) /1000;
            String id = "TeamInfo:" + odsTeam.getTeamId();
            redisTemplate.opsForValue().set(id, odsTeam, expireTime, TimeUnit.SECONDS);
        }

        return odsTeamMapper.insertSelective(odsTeam);
    }

    @Override
    public List<OdsTeamDto> getTeamInfo(OdsTeamDto odsTeamDto11){
        List<OdsTeamDto> odsTeamDtoList = odsTeamMapper.getTeamInfo(odsTeamDto11);
        if (odsTeamDtoList == null || odsTeamDtoList.size() < 1){
            return odsTeamDtoList;
        }

        List<Long> userIds = new ArrayList<>();
        List<Long> warehouseIds = new ArrayList<>();
        List<Long> specIds = new ArrayList<>();
        List<Long> storageIds = new ArrayList<>();

        for (OdsTeamDto teamDto : odsTeamDtoList){
            userIds.add(teamDto.getUserId());
            warehouseIds.add(teamDto.getWarehouseId());
            if (teamDto.getTeamType().equals((byte)1)){
                specIds.add(teamDto.getSpecId());
                storageIds.add(teamDto.getStorageId());
            }
        }
        //获得userName
        Wrapper<List<UserInfoDto>> wrapperUsers = uacUserFeignApi.getUserInfoByUserIds(userIds);

        if (wrapperUsers.getCode() != 200 || wrapperUsers.getResult() == null ){
            return odsTeamDtoList;
        }
        List<UserInfoDto> userInfoDtoList = wrapperUsers.getResult();

        //获得warehouseName
        Wrapper<List<WarehouseInfoDto>> wrapperWarehouse = wmcStorageFeignApi.getWarehouseInfo(warehouseIds);

        if (wrapperWarehouse.getCode() != 200 || wrapperWarehouse.getResult() == null ){
            return odsTeamDtoList;
        }
        List<WarehouseInfoDto> warehouseInfoList = wrapperWarehouse.getResult();

        //拼单显示商品名称
        List<StorageDto> storageDtoList = new ArrayList<>();
        if (storageIds != null && storageIds.size() > 0){
            storageDtoList = wmcStorageFeignApi.getStorageInfoByStorageIds (storageIds);

        }

        //装配
        for (OdsTeamDto item : odsTeamDtoList){
            for(UserInfoDto user : userInfoDtoList){
                if (item.getUserId().equals(user.getUserId())){
                    item.setUserName(user.getUserName());
                }
            }

            for (WarehouseInfoDto warehouse : warehouseInfoList){
                if (item.getWarehouseId().equals(warehouse.getWarehouseId())){
                    item.setWarehouseName(warehouse.getWarehouseName());
                }
            }

            if (storageDtoList != null && storageDtoList.size() > 0){
                for (StorageDto storage : storageDtoList){
                    if (item.getStorageId().equals(storage.getStorageId())){
                        item.setSpecName(storage.getSpecName());
                    }
                }
            }

        }

        return odsTeamDtoList;
    }

    @Override
    public List<OdsTeamRecordDto> selectTeamRecordList(OdsTeamDto odsTeamDto11) {
        List<OdsTeamRecordDto> odsTeamRecordList = new ArrayList<>();
        List<OdsTeamDto> odsTeamDtoList = odsTeamMapper.getTeamInfo(odsTeamDto11);
        if (odsTeamDtoList == null || odsTeamDtoList.size() < 1) {
            return odsTeamRecordList;
        }


        //获取商品列表
        for (OdsTeamDto item : odsTeamDtoList){
            OdsTeamGoodsDto odsTeamGoodsDto = new OdsTeamGoodsDto();
            odsTeamGoodsDto.setTeamId(item.getTeamId());
            //只获取有效商品
            odsTeamGoodsDto.setGoodsStatus((byte)5);
            List<OdsTeamGoodsDto> list = odsTeamGoodsService.selectTeamGoodsByCondition(odsTeamGoodsDto);
            list = odsTeamGoodsService.getExtraInfo(list);

            OdsTeamRecordDto odsTeamRecordDto = new OdsTeamRecordDto();
            BeanUtils.copyProperties(item,odsTeamRecordDto);
            odsTeamRecordDto.setTeamGoodsList(list);
            odsTeamRecordList.add(odsTeamRecordDto);
        }
        return odsTeamRecordList;
    }

    @Override
    public List<OdsTeamInfoDto> selectTeamInfo(OdsTeamInfoDto odsTeamDto) {
        List<OdsTeamInfoDto> list = odsTeamMapper.selectTeamInfo(odsTeamDto);
        return list;
    }

    @Override
    public List<OdsTeamSpecInfoDto> selectTeamInfoBySpecIds(List<Long> specIds, Long warehouseId) {
        List<OdsTeamSpecInfoDto> list = odsTeamMapper.selectTeamInfoBySpecIds(specIds, warehouseId);

        return list;
    }

    @Override
    public List<Map<String, String>> accountTeamIntentMonth(Long shopId){
        List<Map<String, String>> map = odsTeamMapper.accountTeamIntentMonth(shopId);
        return map;
    }

    @Override
    public List<Map<String, String>> accountTeamIntentDay(Long shopId){
        List<Map<String, String>> map = odsTeamMapper.accountTeamIntentDay(shopId);
        return map;
    }


    @Override
    public Map<String, String> getTeamProfit(Long shopId){
        Map<String, String> mapTotal = odsTeamMapper.getTeamProfitTotal(shopId);
        Map<String, String> mapMonth = odsTeamMapper.getTeamProfitMonth(shopId);
        Map<String, String> map = new HashMap<>();
        map.put("commissionTotal",mapTotal.get("commissionTotal"));
        map.put("commissionMonth",mapMonth.get("commissionMonth"));
        return map;
    }
}
